package com.zyf.wjy.dao;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zyf.wjy.entity.News;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;

/**
 * 新闻公告DAO
 */
@Repository
public class NewsDao extends BaseDao<News> {
    
    @Override
    protected String getFileName() {
        return "news.json";
    }
    
    @Override
    protected TypeReference<List<News>> getTypeReference() {
        return new TypeReference<List<News>>() {};
    }
    
    @Override
    protected Long getId(News entity) {
        return entity.getId();
    }
    
    @Override
    protected void setId(News entity, Long id) {
        entity.setId(id);
    }
    
    /**
     * 根据类型查询
     */
    public List<News> findByType(String type) {
        return findAll().stream()
            .filter(news -> type.equals(news.getType()))
            .toList();
    }
    
    /**
     * 查询已发布的新闻（按发布时间倒序）
     */
    public List<News> findPublished() {
        return findAll().stream()
            .filter(news -> news.getStatus() == 1)
            .sorted(Comparator.comparing(News::getPublishTime).reversed())
            .toList();
    }
    
    /**
     * 查询置顶新闻
     */
    public List<News> findTopNews() {
        return findAll().stream()
            .filter(news -> news.getIsTop() == 1 && news.getStatus() == 1)
            .sorted(Comparator.comparing(News::getPublishTime).reversed())
            .toList();
    }
}

